#!/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DROPBEAR_RSAKEY_DEFAULT="@KEYDIR@/dropbear_rsa_host_key"
DROPBEAR_DSSKEY_DEFAULT="@KEYDIR@/dropbear_dss_host_key"

test -n "$DROPBEAR_RSAKEY" || \
  DROPBEAR_RSAKEY=$DROPBEAR_RSAKEY_DEFAULT
test -n "$DROPBEAR_DSSKEY" || \
  DROPBEAR_DSSKEY=$DROPBEAR_DSSKEY_DEFAULT

gen_key() {

	key_type=$1
	key_file=$2

	rm -f $key_file > /dev/null 2>&1

	echo -n "generating $key_type key..."
	dropbearkey -t $key_type -f $key_file > /dev/null 2>&1

	if [ "$?" = "0" ]; then
		echo "done"
	else
		echo "failed"
		exit 1
	fi
}

[ -e "$DROPBEAR_RSAKEY" ] || gen_key rsa "$DROPBEAR_RSAKEY"
[ -e "$DROPBEAR_DSSKEY" ] || gen_key dss "$DROPBEAR_DSSKEY"

